<script setup lang="ts">
import type { FormInstance } from 'element-plus'
import { ElMessage } from 'element-plus'
import type { FormType } from './index'
import { GetDetailData, PostSysshareconfigData, PutModifyData } from '@/api/modules/sysshareconfig'
import detailStore from '@/store/modules/NewCacheData'

const router = useRouter()
const route = useRoute()
const detailData = detailStore()

// const imgDomain = 'https://img1.eprhan.cc'

const ruleFormRef = ref<FormInstance>()
const ruleForm = ref<FormType>({
  actId: 0,
  shareTxt: '',
  shareImg: '',
})

const actTypeOptionList = [
  {
    value: 1,
    label: '连连看',
  },
]

const actId = route.query.actId as string
const id = route.query.id as string

// 提交
async function SubmitData() {
  const params = { ...ruleForm.value }
  if (id) {
    // 编辑
    params.id = id
    PutModifyData(params).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('编辑成功')
        router.push({ name: 'sysshareconfigIndex' })
      }
    })
  }
  else {
    // 添加
    PostSysshareconfigData(params).then((res: any) => {
      if (res.statusCode === 200) {
        ElMessage.success('添加成功')
        detailData.emptyFormData('sysshareconfigFrom')
        router.push({ name: 'sysshareconfigIndex' })
      }
    })
  }
}

// 图片上传回调
function handleUploadSuccess(url: string) {
  if (url) {
    ruleForm.value.shareImg = url
  }
}

onMounted(async () => {
  if (id) {
    await GetDetailData(id).then(async (res: any) => {
      if (res.statusCode === 200) {
        ruleForm.value = res.data
      }
    })
  }
  else {
    ruleForm.value = detailData.getFormData('sysshareconfigFrom')
  }

  if (actId) {
    ruleForm.value.actId = Number(actId)
  }

  watch(ruleForm, (newValue) => {
    if (!id) {
      detailData.rememberFormData('sysshareconfigFrom', newValue)
    }
  }, { deep: true })
})
</script>

<template>
  <div>
    <PageHeader :title="id ? '编辑' : '添加'" />
    <ElForm ref="ruleFormRef" label-position="top" label-width="80px" :model="ruleForm">
      <PageMain title="基础信息">
        <ElRow :gutter="20">
          <ElCol :md="24">
            <el-form-item label="活动">
              <el-select v-model="ruleForm.actId" placeholder="请选择活动" :disabled="actId ? true : false">
                <el-option v-for="item in actTypeOptionList" :key="item.value" :label="item.label" :value="item.value" />
              </el-select>
            </el-form-item>
          </ElCol>
          <ElCol :md="16">
            <ElFormItem label="分享语">
              <el-input v-model="ruleForm.shareTxt" placeholder="请输入分享语" type="textarea" style="max-width: 500px;" />
            </ElFormItem>
          </ElCol>
          <ElCol :md="24">
            <ElFormItem label="分享图">
              <ImageUpload
                :size="1" width="auto" :height="150" :url="ruleForm.shareImg" :ext="['png', 'jpg']"
                @on-success="(imgUrl) => handleUploadSuccess(imgUrl)"
              />
            </ElFormItem>
          </ElCol>
        </ElRow>
      </PageMain>
    </ElForm>
    <div style="height: 80px;" />
    <FixedActionBar>
      <ElButton type="primary" size="large" @click="SubmitData()">
        提交
      </ElButton>
    </FixedActionBar>
  </div>
</template>

<style lang="scss" scoped>
.box {
  .title {
    display: flex;
    justify-content: space-between;
    margin-bottom: 10px;
  }
}

.card-box {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
}
</style>
